package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.itheima.dao.CommentMapper;
import com.itheima.dao.UserMapper;
import com.itheima.pojo.*;
import com.itheima.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;

/**
 * @ClassName CommentServiceImpl
 * @Description 评论表 服务实现类
 * @Author pengkaka
 * @Date 2020/8/4 18:46
 * @Version 1.0
 **/
@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentMapper commentMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public Integer getCommentCount(int status) {
        Example example = new Example(Comment.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", status);
        Integer count = commentMapper.selectCountByExample(example);
        return count;
    }
    // 生成满足要求的32位uid的私有方法
    private String getNewUID() {
        return UUID.randomUUID().toString().toLowerCase().replaceAll("-", "");
    }

    //原版
    @Override
    public String add(Comment comment) {
        // 这里要判断该评论是否为一级评论,并以此添加状态
        int insert = commentMapper.insert(comment);
        if (insert == 0) {
            return "failed";
        }
        return "succeed";
    }
    
    //@Override
    //public Comment add(Comment comment) {
    //    // 这里要判断该评论是否为一级评论,并以此添加状态
    //    try {
    //        //生成uid并赋值和插入
    //        commentMapper.insert(comment);
    //        //需要重新添加firstCommentUid,user对象,uid,(status,createTime,updateTime 这几个自动生成)
    //        //查询当前评论是否为一级评论
    //        //查询user对象
    //        String userUid = comment.getUserUid();
    //        String toUserUid = comment.getToUserUid();
    //    } catch (Exception e) {
    //        e.printStackTrace();
    //    }
    //    return comment;
    //}
    
    

    @Override
    public String delete(Comment comment) {
        // 设置状态为已删除
        // 默认为1,默认未删除,故0/false为已删除
        comment.setStatus(false);
        int update = commentMapper.updateByPrimaryKeySelective(comment);
        if (update == 0) {
            return "failed";
        }
        return "succeed";
    }

    @Override
    public String deleteBatch(List<Comment> commentList) {
        for (Comment comment : commentList) {
            comment.setStatus(false);
            int update = commentMapper.updateByPrimaryKeySelective(comment);
        }
        return "succeed";
    }

    @Override
    public String edit(Comment comment) {
        int update = commentMapper.updateByPrimaryKeySelective(comment);
        if (update == 0) {
            return "failed";
        }
        return "succeed";
    }
    
    
    @Override
    public Map<String,Object> getPageList(Map<String, Object> searchMap) {
        Example example = new Example(Comment.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status",1);
        // 判断当前页
        Integer currentPage = (Integer) searchMap.get("currentPage");
        if (currentPage == null) {
            currentPage = 1;
        }
        // 判断每页显示条数
        Integer pageSize = (Integer) searchMap.get("pageSize");
        if (pageSize == null) {
            pageSize = 10;
        }
        // 判断是否含关键字
        String keyword = (String) searchMap.get("keyword");
        if (keyword != null) {
            criteria.andLike("content","%"+keyword+"%");
        }
        // 判断评论人,传来的userName实际是nickName
        String userName = (String) searchMap.get("userName");
        if (userName != null) {
            // 根据nickName查询userUid
            Example temExample = new Example(User.class);
            temExample.createCriteria().andEqualTo("nickName", userName);
            List<User> users = userMapper.selectByExample(temExample);
            criteria.andEqualTo("userUid",users.get(0).getUid());
        }
        example.setOrderByClause("`update_time` DESC");
        //在分页前获取总数
        List<Comment> listForTotal = commentMapper.selectByExample(example);
        PageHelper.startPage(currentPage, pageSize);
        List<Comment> commentList = commentMapper.selectByExample(example);
        List<Comment> newList = new ArrayList<>();
        // 得到评论人和被评论人
        for (Comment comment : commentList) {
            // 根据userUid来查询user对象
            User forSearchUser = new User();
            forSearchUser.setUid(comment.getUserUid());
            User user = userMapper.selectByPrimaryKey(forSearchUser);
            // 进行该评论的用户可能已经不存在
            if (user == null) {
                // 对当前操作的用户进行提示
                User temUser = new User();
                temUser.setNickName("(该用户已注销)");
                comment.setUser(temUser);
                // 此时不必查询他人对 这个已注销用户的回复,结束循环
                newList.add(comment);
                continue;
            }
            comment.setUser(user);
            // 根据toUserId来查询toUser对象
            User forSearchToUser = new User();
            forSearchToUser.setUid(comment.getToUserUid());
            User toUser = userMapper.selectByPrimaryKey(forSearchToUser);
            // 判断该评论是否为一级评论
            if (toUser == null) {
                // 若为一级评论则添加评论对象并结束本次循环
                newList.add(comment);
                continue;
            }
            comment.setToUser(toUser);
            // 添加对象到新集合
            newList.add(comment);
        }
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("list", newList);
        resultMap.put("pageNum", currentPage);
        resultMap.put("total", listForTotal.size());
        return resultMap;
    }
    
    
    
    //@Override
    //public PageInfo<Comment> getPageList(Map<String, Object> searchMap) {
    //    Example example = new Example(Comment.class);
    //    Example.Criteria criteria = example.createCriteria();
    //    criteria.andEqualTo("status",1);
    //    // 判断当前页
    //    Integer currentPage = (Integer) searchMap.get("currentPage");
    //    if (currentPage == null) {
    //        currentPage = 1;
    //    }
    //    // 判断每页显示条数
    //    Integer pageSize = (Integer) searchMap.get("pageSize");
    //    if (pageSize == null) {
    //        pageSize = 10;
    //    }
    //    // 判断是否含关键字
    //    String keyword = (String) searchMap.get("keyword");
    //    if (keyword != null) {
    //        criteria.andLike("content","%"+keyword+"%");
    //    }
    //    // 判断评论人,传来的userName实际是nickName
    //    String userName = (String) searchMap.get("userName");
    //    if (userName != null) {
    //        // 根据nickName查询userUid
    //        Example temExample = new Example(User.class);
    //        temExample.createCriteria().andEqualTo("nickName", userName);
    //        List<User> users = userMapper.selectByExample(temExample);
    //        criteria.andEqualTo("userUid",users.get(0).getUid());
    //    }
    //    example.setOrderByClause("`update_time` DESC");
    //    //在分页前获取总数
    //    List<Comment> listForTotal = commentMapper.selectByExample(example);
    //    PageHelper.startPage(currentPage, pageSize);
    //    List<Comment> commentList = commentMapper.selectByExample(example);
    //    List<Comment> newList = new ArrayList<>();
    //    // 得到评论人和被评论人
    //    for (Comment comment : commentList) {
    //        // 根据userUid来查询user对象
    //        User forSearchUser = new User();
    //        forSearchUser.setUid(comment.getUserUid());
    //        User user = userMapper.selectByPrimaryKey(forSearchUser);
    //        // 进行该评论的用户可能已经不存在
    //        if (user == null) {
    //            // 对当前操作的用户进行提示
    //            User temUser = new User();
    //            temUser.setNickName("(该用户已注销)");
    //            comment.setUser(temUser);
    //            // 此时不必查询他人对 这个已注销用户的回复,结束循环
    //            newList.add(comment);
    //            continue;
    //        }
    //        comment.setUser(user);
    //        // 根据toUserId来查询toUser对象
    //        User forSearchToUser = new User();
    //        forSearchToUser.setUid(comment.getToUserUid());
    //        User toUser = userMapper.selectByPrimaryKey(forSearchToUser);
    //        // 判断该评论是否为一级评论
    //        if (toUser == null) {
    //            // 若为一级评论则添加评论对象并结束本次循环
    //            newList.add(comment);
    //            continue;
    //        }
    //        comment.setToUser(toUser);
    //        // 添加对象到新集合
    //        newList.add(comment);
    //    }
    //    // 设置pageInfo对象
    //    PageInfo<Comment> pageInfo = new PageInfo<>(newList);
    //    pageInfo.setPageNum(currentPage);
    //    pageInfo.setTotal(listForTotal.size());
    //    return pageInfo;
    //}
}
